package com.news.servlet.admin;

import com.news.model.Sort;
import com.news.service.NewsService;
import com.news.service.impl.NewsServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/admin/categoryAdd")
public class CategoryAddServlet extends HttpServlet {
    private NewsService newsService = new NewsServiceImpl();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            // 获取表单数据
            String name = request.getParameter("name");

            // 创建分类对象
            Sort sort = new Sort();
            sort.setName(name);

            // 添加分类
            if (newsService.addSort(sort)) {
                request.getSession().setAttribute("message", "分类添加成功！");
                response.sendRedirect(request.getContextPath() + "/admin/categoryManage");
            } else {
                request.getSession().setAttribute("message", "分类添加失败！");
                response.sendRedirect(request.getContextPath() + "/admin/category_add.jsp");
            }
        } catch (Exception e) {
            request.getSession().setAttribute("message", "分类添加失败：" + e.getMessage());
            response.sendRedirect(request.getContextPath() + "/admin/category_add.jsp");
        }
    }
} 